package com.message.api.log.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.base.helper.StringUtils;
import com.message.api.log.dao.AcVisitsMapper;
import com.message.api.log.entity.AcVisits;
import com.message.api.log.service.AcVisitsService;
import com.message.api.log.vo.AcVisitsVo;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mzl
 * @since 2020-07-09
 */
@Service
public class AcVisitsServiceImpl extends ServiceImpl<AcVisitsMapper, AcVisits> implements AcVisitsService {

    @Override
    public List<AcVisitsVo> findByExample(AcVisitsVo example) {
        return baseMapper.findByExample(buildWrapper(example));
    }

    @Override
    public Page<AcVisitsVo> findByPage(AcVisitsVo example, Page page) {
        page.setOptimizeCountSql(false);
        return page.setRecords(baseMapper.findByExample(buildWrapper(example), page));
    }

    private QueryWrapper<AcVisitsVo> buildWrapper(AcVisitsVo example) {
        QueryWrapper<AcVisitsVo> wrapper        = new QueryWrapper<>(example);
        String                   acoperator     = example.getAcoperator();
        String                   acoperatorName = example.getAcoperatorName();
        String                   grouptypeid    = example.getGrouptypeid();
        String                   grouptypeName  = example.getGrouptypeName();
        String                   usergroup      = example.getUsergroup();
        String                   usergroupName  = example.getUsergroupName();
        wrapper.like(StringUtils.isNotEmpty(acoperatorName), "acoperator_name", acoperatorName)
                .like(StringUtils.isNotEmpty(grouptypeName), "grouptype_name", grouptypeName)
                .like(StringUtils.isNotEmpty(usergroupName), "usergroup_name", usergroupName);
        wrapper.lambda()
                .eq(null != acoperator, AcVisitsVo::getAcoperator, acoperator)
                .eq(StringUtils.isNotEmpty(grouptypeid), AcVisitsVo::getGrouptypeid, grouptypeid)
                .eq(StringUtils.isNotEmpty(usergroup), AcVisitsVo::getUsergroup, usergroup)
                .orderByAsc(AcVisitsVo::getId);
        return wrapper;
    }

}
